package com.yanqu.road.server.logic.activity.condition.fellingtrees;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.FellingTreesLevelArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseNewRankActivityCondition;
import com.yanqu.road.server.manger.activity.fellingtrees.FellingTreesMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

public class FellingTreesLevelRankCondition extends BaseNewRankActivityCondition {

    public FellingTreesLevelRankCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        FellingTreesLevelArgs args = (FellingTreesLevelArgs) object;

        boolean change = false;

        // 等级变化
        BigInteger level = BigInteger.valueOf(args.getLevel());
        if (level.compareTo(getValue()) > 0) {
            setValue(level);
            change = true;
        }

        // 当前战力
        String equipData = args.getEquipData();
        BigInteger attack = new BigInteger(equipData.substring(0, equipData.indexOf('#')));

        // 之前战力
        BigInteger oldAttack = BigInteger.ZERO;
        if (!StringUtils.isNullOrEmpty(getParam())) {
            oldAttack = new BigInteger(getParam());
        }

        // 战力变化
        if (attack.compareTo(oldAttack) > 0) {
            FellingTreesMgr.updateRankEquip(activityInfo.getActivityId(), player.getUserId(), equipData);
            setParam(attack.toString());
            change = true;
        }

        // 榜单
        if (change) {
            rankChange();
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.FellingTreesLevelRank.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.FellingTreesLevelRank.getValue(), handle);
    }

}
